<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
  >
    <template slot="footer">
      <a-button key="back" @click="handleCancel">取消</a-button>
      <a-button type="primary" v-if="!disableSubmit" @click="chongZhi">重置</a-button>
      <a-button key="submit" type="primary" v-if="!disableSubmit" @click="handleOk">保存</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12">
            <a-form-item label="目录号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="isF||disableSubmit"
                v-decorator="['muLuHao',{
            rules: [
              {
                required: true,
                message: '目录号不能为空',
              },
            ],
          }]"
                placeholder="请输入目录号"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="立档单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart
                :disabled="isF||disableSubmit"
                v-decorator="['liDangDanWei',{
            rules: [
              {
                required: true,
                message: '请选择立档单位',
              },
            ],
          }]"
                :trigger-change="true"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="归档机构" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart
                :disabled="isF||disableSubmit"
                v-decorator="['guiDangJiGou',{
            rules: [
              {
                required: true,
                message: '请选择归档机构',
              },
            ],
          }]"
                :trigger-change="true"
              ></j-select-depart>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="归档年度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="isF||disableSubmit"
                type="list"
                v-decorator="[
                  'nianDu',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请选择归档年度'
                      }
                    ]
                  }
                ]"
                :trigger-change="true"
                dictCode="NianDu"
                placeholder="请选择归档年度"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="保管期限" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="isF||disableSubmit"
                type="list"
                v-decorator="['baoGuanQiXian',{
            rules: [
              {
                required: true,
                message: '请选择保管期限',
              },
            ],
          }]"
                :trigger-change="true"
                dictCode="BaoGuanQiXian"
                placeholder="请选择保管期限"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="档号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="true" v-decorator="['dangHao']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="件号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="true" v-decorator="['anJuanHao']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="密级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="isF||disableSubmit"
                type="list"
                v-decorator="['miJi',{
            rules: [
              {
                required: true,
                message: '请选择密级',
              },
            ],
          }]"
                :trigger-change="true"
                dictCode="MiJi"
                placeholder="请选择密级"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="立件日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择立件日期"
                v-decorator="['liJuanRiQi',{
            rules: [
              {
                required: true,
                message: '请选择立卷日期',
              },
            ],
          }]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="页数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number
                :disabled="isF||disableSubmit"
                style="width: 100%"
                placeholder="请输入页数"
                v-decorator="['yeShu']"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="题名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                v-decorator="['tiMing',{
                    rules: [
                      {
                        required: true,
                        message: '题名不能为空'
                      }
                    ]
                  }]"
                placeholder="请输入题名"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="日期(起始)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择日期(起始)"
                v-decorator="['riQiQiShi',
                {
                    rules: [
                      {
                        required: true,
                        message: '请选择日期(起始)'
                      }
                    ]
                  }]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="日期(终止)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择日期(终止)"
                v-decorator="['riQiZhongZhi',
                {
                    rules: [
                      {
                        required: true,
                        message: '请选择日期(终止)'
                      }
                    ]
                  }]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="全宗号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="disableSubmit"
                @click.native="showModalQZ"
                v-decorator="[
                  'quanZongHao',
                  {
                    rules: [
                      {
                        required: true,
                        message: '全宗号不能为空'
                      }
                    ]
                  }
                ]"
                placeholder="请输入全宗号"
              ></a-input>
            </a-form-item>
            <a-modal
              centered
              title="全宗号"
              :visible="visibleQZ"
              @ok="handleOkQZ"
              @cancel="handleCancelQZ"
              width="900px"
            >
              <a-row :gutter="24">
                <div class="table-page-search-wrapper">
                  <a-form layout="inline">
                    <a-row :gutter="24">
                      <a-col :span="14">
                        <a-form-item label="全宗名称">
                          <a-input placeholder="请输入" v-model="searchQZ" />
                        </a-form-item>
                      </a-col>
                      <a-col :span="8">
                        <span
                          style="float: left;overflow: hidden;"
                          class="table-page-search-submitButtons"
                        >
                          <a-button type="primary" icon="search" @click.native="search()">查询</a-button>
                          <a-button
                            type="primary"
                            icon="reload"
                            @click.native="reset()"
                            style="margin-left: 8px"
                          >重置</a-button>
                        </span>
                      </a-col>
                    </a-row>
                  </a-form>
                </div>
                <a-table
                  bordered
                  rowKey="id"
                  :columns="columnsQZ"
                  :dataSource="dataQZ"
                  :loading="loading"
                  :pagination="false"
                >
                  <template slot="action" slot-scope="text, record">
                    <a @click="() => seve(record)">选择</a>
                  </template>
                </a-table>
                <a-pagination
                  style="margin-top:20px;float:right"
                  showQuickJumper
                  :defaultCurrent="1"
                  :total="total"
                  @change="onChangeQZ"
                />
              </a-row>
            </a-modal>
          </a-col>
          <a-col :md="12">
            <a-form-item label="存放位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="true" v-decorator="['cunFangWeiZhiMingCheng']"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="流转状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="true"
                type="list"
                v-decorator="['liuZhuanZhuangTai']"
                :trigger-change="true"
                dictCode="liuZhuanZhuangTai"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12">
            <a-form-item label="上架状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="true"
                type="list"
                v-decorator="['shangJiaZhuangTai']"
                :trigger-change="true"
                dictCode="ShangJiaZhuangTai"
              />
            </a-form-item>
          </a-col>
          <a-col :md="24">
            <a-form-item label="备注" v-bind="formItemLayout">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['beiZhu']"
                rows="4"
                placeholder="请输入备注"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction, getAction, postAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
export default {
  name: 'DaJianModal',
  components: {
    JDate,
    JDictSelectTag,
    JSelectDepart,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      disableSubmit: false,
      model: {},
      newmodel: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
      },
      confirmLoading: false,
      validatorRules: {},
      url: {
        add: '/daJian/add',
        edit: '/daJian/edit',
      },
      isF: false,
      visibleQZ: false,
      loading: true,
      dataQZ: [],
      searchQZ: '',
      columnsQZ: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 80,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        { title: '全宗名称', dataIndex: 'mingCheng', align: 'center' },
        { title: '全宗号', dataIndex: 'bianMa', align: 'center' },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 120,
          scopedSlots: { customRender: 'action' },
        },
      ],
      total: 0,
    }
  },
  created() {},
  methods: {
    // 全宗
    // 弹窗
    showModalQZ() {
      this.visibleQZ = true
      this.searchQZ = ''
      getAction(`/xtQuanZong/list`, {
        pageNo: 1,
        pageSize: 5,
      }).then((res) => {
        if (res.success) {
          this.dataQZ = res.result.records
          this.total = res.result.total
          this.loading = false
        }
      })
    },
    // 确定
    handleOkQZ(e) {
      this.visibleQZ = false
    },
    // 取消
    handleCancelQZ(e) {
      this.visibleQZ = false
    },
    // 分页
    onChangeQZ(pageNumber) {
      getAction(`/xtQuanZong/list`, {
        pageNo: pageNumber,
        pageSize: 5,
        mingCheng: this.searchQZ,
      }).then((res) => {
        if (res.success) {
          this.dataQZ = res.result.records
          this.loading = false
        }
      })
    },
    // 查询
    search() {
      getAction(`/xtQuanZong/list`, {
        mingCheng: this.searchQZ,
        pageNo: 1,
        pageSize: 5,
      }).then((res) => {
        if (res.success) {
          this.dataQZ = res.result.records
          this.loading = false
          this.total = res.result.total
        }
      })
    },
    // 重置
    reset() {
      this.searchQZ = ''
    },
    // 选择
    seve(record) {
      this.visibleQZ = false
      this.$nextTick(() => {
        this.form.setFieldsValue({
          quanZongHao: record.bianMa,
        })
      })
    },
    // 重置
    chongZhi() {
      this.form.resetFields()
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.newmodel,
            'muLuHao',
            'liDangDanWei',
            'guiDangJiGou',
            'nianDu',
            'baoGuanQiXian',
            'dangHao',
            'anJuanHao',
            'liJuanRiQi',
            'yeShu',
            'tiMing',
            'miJi',
            'riQiQiShi',
            'riQiZhongZhi',
            'beiZhu',
            'quanZongHao',
            'liuZhuanZhuangTai',
            'cunFangWeiZhiMingCheng'
          )
        )
      })
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.newmodel = Object.assign({}, record)
      this.visible = true
      if (record.count > 0) {
        this.isF = true
      }
      if (!record.liJuanRiQi) {
        let newDate = new Date()
        this.$nextTick(() => {
          this.form.setFieldsValue({
            liJuanRiQi: newDate.getFullYear() + '-' + (newDate.getMonth() + 1) + '-' + newDate.getDate(),
          })
        })
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'muLuHao',
            'liDangDanWei',
            'guiDangJiGou',
            'nianDu',
            'baoGuanQiXian',
            'dangHao',
            'anJuanHao',
            'liJuanRiQi',
            'yeShu',
            'tiMing',
            'miJi',
            'riQiQiShi',
            'riQiZhongZhi',
            'beiZhu',
            'quanZongHao',
            'liuZhuanZhuangTai',
            'cunFangWeiZhiMingCheng',
            'shangJiaZhuangTai'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.isF = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          values.menLeiHangBiaoShi = 'WENSHU'
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.confirmLoading = false
                that.close()
              } else {
                that.$message.warning(res.message)
                that.confirmLoading = false
              }
            })
            .finally(() => {})
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'muLuHao',
          'liDangDanWei',
          'guiDangJiGou',
          'nianDu',
          'baoGuanQiXian',
          'dangHao',
          'anJuanHao',
          'liJuanRiQi',
          'yeShu',
          'tiMing',
          'miJi',
          'riQiQiShi',
          'riQiZhongZhi',
          'beiZhu',
          'quanZongHao',
          'shangJiaZhuangTai'
        )
      )
    },
  },
}
</script>